<?php
namespace osc\admin\controller;
use osc\common\controller\Base;
use think\Db;
class AdminBase extends Base{	
	
	protected $user;
	
	protected function _initialize() {
		
		$user=new \oscshop\User();
		
		$this->user=$user;
		
		define('UID',$user->is_login());
        if(!UID){  
			$this->redirect('Login/login');
        }
	
        $config =   cache('DB_CONFIG_DATA');
        if(!$config){
            $config =   api('Config/lists');
            cache('DB_CONFIG_DATA',$config);
        }
        config($config); 
	
		$this->get_menu();
        
        //权限判断 
        /*
        if(session('user_auth.username')!=cache('administrator')){//超级管理员不需要验证        
	        
			$auth = new \auth\Auth();
			
			if (!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME, session('user_auth.uid'))) {
				$this->error('没有权限！');
			}
		}
		*/
	}
	
	public function get_menu(){
		
		$key='user_admin_menu_cache'.session('user_auth.group_id');
		
		if (!$user_admin_menu_cache = cache($key)) {		

			$menu=Db::name('auth_rule')->where('group_id',session('user_auth.group_id'))->select();
		
			if(empty($menu)){
				
				$group_menu=Db::name('auth_group')->where('id',session('user_auth.group_id'))->find();			
		
				$menu_id=explode(',', $group_menu['rules']);	
				
				$menu_list=Db::name('menu')->select();
				
				foreach ($menu_id as $k => $v) {
					
					foreach ($menu_list as $k1 => $v1) {
						
						if($v==$v1['id']){
							$m['group_id']=session('user_auth.group_id');
							$m['menu_id']=$v;					
							$m['name']=!empty($v1['url'])?'admin/'.$v1['url']:'';
							Db::name('auth_rule')->insert($m);
						}
						
					}					
					
				}
							
			}
			$admin_menu=Db::query('select m.* from '.config('database.prefix').'auth_rule ar,'.config('database.prefix').'menu m where m.id=ar.menu_id and ar.group_id='.session('user_auth.group_id').' order by m.sort_order');
			
			$user_admin_menu_cache=list_to_tree($admin_menu,'id','pid','children',0);
			
			cache($key, $user_admin_menu_cache);
		}
		$this->assign('admin_menu',$user_admin_menu_cache);
		
	}
	

}
